package top.raoqq.sky.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


/**
 * @author 饶奇奇
 * @date 2023/8/4
 */
@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    private  ValueOperations<String, Object> stringCache;

    @PostConstruct
    public void init() {
        stringCache = redisTemplate.opsForValue();
    }
    /**
     * 判断key是否存在
     */
    public boolean hasKey(final String key) {
         if (StringUtils.isBlank(key)) {
             return false;
         }
         return redisTemplate.hasKey(key);
    }

    /**
     * 设置缓存
     */
    public void setCache(final String key, final Object value, final long timeout, final  TimeUnit timeUnit) {
      stringCache.set(key, value, timeout, timeUnit);
    }

    public Object getCache(final String key) {
      return stringCache.get(key);
    }

    public void incr(final String key) {
        stringCache.increment(key);
    }

    public void deleteCache(final String key) {
        redisTemplate.delete(key);
    }

}
